<?php

namespace App\Http\Controllers\Backend;

use App\Http\Requests\Backend\StoreWechatMenu;
use App\Http\Resources\Backend\WechatMenuResource;
use App\Models\AdminLog;
use App\Models\WechatMenu;
use App\Services\WechatMenuService;

/**
 * Class WechatMenuController
 * @package App\Http\Controllers\Backend
 */
class WechatMenuController extends BackendController
{
    /**
     * @param StoreWechatMenu $request
     * @return WechatMenuResource
     * 添加菜单
     */
    public function store(StoreWechatMenu $request)
    {
        $wechatMenu = WechatMenuService::add($request);
        AdminLog::log('新增菜单' . $wechatMenu->name);
        return new WechatMenuResource($wechatMenu);
    }

    /**
     * @param WechatMenu $wechatMenu
     * @return WechatMenuResource|array
     * 编辑菜单
     */
    public function update(WechatMenu $wechatMenu)
    {
        try {
            $wechatMenu = WechatMenuService::edit($wechatMenu);
            AdminLog::log('编辑菜单' . $wechatMenu->name);
            return new WechatMenuResource($wechatMenu);
        } catch (\Exception $exception) {
            return $this->fail($exception->getMessage());
        }
    }

    /**
     * @param WechatMenu $wechatMenu
     * @return WechatMenuResource
     * @throws \Exception
     * 删除子菜单
     */
    public function destroy(WechatMenu $wechatMenu)
    {
        $wechatMenu = WechatMenuService::del($wechatMenu);
        AdminLog::log('删除菜单' . $wechatMenu->id);
        return new WechatMenuResource($wechatMenu);
    }
}